/**
 * 
 */
package com.xbang.attendance.web.action.system.region;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.system.Region;
import com.xbang.attendance.service.SystemService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.struts.support.BaseActionSupport;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class RegionManager extends BaseActionSupport {

	private static final long serialVersionUID = 6843098369301424790L;

	private static final Log logger = LogFactory.getLog(RegionManager.class);

	private List<Region> parentRegions = new ArrayList<Region>();

	private Region region;

	private Integer id;

	@Autowired
	private SystemService systemService;

	@Action("/system/region/add")
	public String add() throws Exception {

		parentRegions = systemService.getObjects(Region.class);

		return "detail";
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/system/region/${id}/view")
	public String view() throws Exception {

		if (region == null && null != id) {
			
			region = (Region) systemService.getObject(Region.class, id);
		}

		return "detail";
	}
	
	@Action("/system/region/${id}/delete")
	public String delete() throws Exception {

		if (null != id) {
			Region tmpRegion  = (Region) systemService.getObject(Region.class, id);
			systemService.delete(tmpRegion);
		}

		return "r:/system/region/list";
	}

	@Action("/system/region/${id}/edit")
	public String edit() throws Exception {

		if (region == null && null != id) {
			region = (Region) systemService.getObject(Region.class, id);
		}
		parentRegions = systemService.getObjects(Region.class);

		return "detail";
	}

	@Action("/system/region/save")
	public String save() throws Exception {

		if (null != region) {
			if (region.getParent() != null && region.getParent().getId() == null){
				region.setParent(null);
				System.out.println(">>>>>>>>>>>>.");
			}
			if (region.getId() != null) {
				systemService.update(region);
			} else
				systemService.save(region);
		}

		return "r:/system/region/list";
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the parentRegions
	 */
	public List<Region> getParentRegions() {
		return parentRegions;
	}

	/**
	 * @param parentRegions
	 *            the parentRegions to set
	 */
	public void setParentRegions(List<Region> parentRegions) {
		this.parentRegions = parentRegions;
	}

	/**
	 * @return the region
	 */
	public Region getRegion() {
		return region;
	}

	/**
	 * @param region
	 *            the region to set
	 */
	public void setRegion(Region region) {
		this.region = region;
	}

	/**
	 * @param systemService
	 *            the systemService to set
	 */
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}


}
